let App = getApp();
Component({
    /**
     * 组件的属性列表
     */
    options: {
        addGlobalClass: true,
    },

    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        ismask: false,
        userPassword: false,
        student_id: '',
        pass_word: '',
    },
    lifetimes: {
        // 组件所在页面的生命周期函数
        created: function() {

            this.setData({
                userPassword: false,
            })

            let token = wx.getStorageSync('token');
            // console.log(token)
            if (token) {
                let page = getApp().curPage();
                page && typeof page.onLogin === 'function' && page.onLogin();
            }
            
            let authinfo = wx.getStorageSync('authinfo');
            // console.log(authinfo)
            // console.log(!authinfo)
            if(!authinfo){
                this.setUserInfo().then((ret) => {
                    wx.setStorageSync('authinfo', ret);
                })
            }
        },

    },
    
    /**
     * 组件的方法列表
     */
    methods: {
        //获取手机号授权
        getPhoneNumber: function(e) {
            console.log(e)
            let _this = this;
            wx.login({
                success(res) {
                    if (res.code) {
                        App._get('/api/member/userLogin', {
                            code: res.code
                        }, (response) => {
                            if (e.detail.encryptedData && e.detail.iv) {
                                App._post('/api/decrypt/decryptData', {
                                    encryptedData: e.detail.encryptedData,
                                    iv: e.detail.iv,
                                    session_key: response.data.data.session_key
                                }, (resp) => {
                                    _this.setData({
                                        phone: resp.data.data.phoneNumber,
                                    })
                                return;
                                });
                            }
                        });
                    }
                }
            })
        },

        getUserProfile(e) {
            console.log(e);
            this.setUserProfile().then(() => {
                let page = App.curPage();
                typeof page.onLogin === 'function' && page.onLogin();
            }).catch(err => {
                getApp().showTips(err)
            })
            this.hide()
        },
        
        setUserInfo(){
           return new Promise((resolve, reject) => {
            wx.login({
                success(res) {
                    console.log(res)
                    if (res.code) {
                        let postdata = {code: res.code} ;
                        wx.getUserInfo({
                            withCredentials: true,
                            success(ret){
                                Object.assign(postdata, ret) 
                                delete postdata.userInfo
                                App._post('/api/wechat/setUserInfo', postdata, (res) => {
                                    if(res.data.code == 200){
                                    //登录完成
                                    resolve(res.data.data)
                                    }else{
                                        reject('登录失败');
                                    }
                                  
                                })
                            },fail(res){
                                console.log('fail', res)
                                reject('获取用户uuid失败')
                            }
                        })
                    }else{
                        reject('用户登录失败')
                    }
                },fail(){
                    reject('用户登录失败!')
                }
            })
           }) 
           
         
        },
        setUserProfile(){
            let authinfo = wx.getStorageSync('authinfo');
            return new Promise((resolve, reject) => {
                wx.getUserProfile({
                    lang:"zh_CN",
                    desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                    success: (event) => {
                        if(event.errMsg == 'getUserProfile:ok') {
                            getApp().globalData.userinfo = event.userInfo;
                            wx.setStorageSync('userinfo', event.userInfo);
                            let postdata = {
                                encryptedData: event.encryptedData,
                                iv: event.iv,
                                session_key: authinfo.session_key,
                                token: authinfo.token,
                                phone: this.data.phone
                            };
                            App._post('/api/wechat/setUserProfile', postdata, (res) => {
                                //设置用户信息完成
                                if(res.data.code == 200){
                                    wx.setStorageSync('token', authinfo.token);
                                    resolve()
                                }else{
                                    reject('用户信息设置失败');
                                }
                            })
    
                        } else {
                            reject('您拒绝了用户信息授权');
                        }
                    },fail(e){
                        // console.log(e)
                        wx.openSetting()
                    }
                  })
            })
            
        },

        show: function(callback) {
            let token = wx.getStorageSync('token');
            if (!token) {
                this.setData({
                    ismask: true,
                })
            } else {
                typeof callback === 'function' && callback();
                this.setData({
                    ismask: false,
                })
            }
        },
        hide: function() {
            this.setData({
                ismask: false,
            })
        },

        showStudentIdLogin: function (){
            this.setData({
                userPassword: true,
            })
        },
        hideStudentIdLogin: function (){
            this.setData({
                userPassword: false,
            })
        },

        set_student_id: function (e){
            let that = this;
            that.setData({
                student_id: e.detail.value
            })
        },
        set_pass_word: function (e){
            let that = this;
            that.setData({
                pass_word: e.detail.value
            })
        },

        // 账号密码登陆
        StudentIdLogin: function (){
            let that = this;
            console.log(that.data.student_id);
            console.log(that.data.pass_word);

            let postdata = {
                student_no:that.data.student_id,
                pwd:that.data.pass_word,
            };
            App._post('/api/login/student/studentNoLogin', postdata, (res) => {
                //设置用户信息完成
                if(res.data.code == 200){
                    console.log(res.data.data)
                    if ('openid' in res.data.data){
                        const {openid} = res.data.data;
                        wx.setStorageSync('token', openid);
                        this.hide();
                    }
                    let page = App.curPage();
                    typeof page.onLogin === 'function' && page.onLogin();
                    App.showSuccess(res.data.data.msg,()=>{})
                    
                }else{
                   App.showError(res.data.data.msg,Function);
                }
            })
        },

    }
})